/**
 * Created by think on 2016/9/12.
 */
angular.module('myApp', ['ui.bootstrap', 'ui.router'])

    .controller('LoginCtrl', ['$http', '$scope', '$state', function ($http, $scope, $state) {

        $scope.submit = function () {
            var url = '/accounts/login/';
            var config = {};

            console.log("post:" + JSON.stringify($scope.user));
            $http.post(url, $scope.user, config).then(function successCallback(response) {
                var status_code = response.data.status_code;
                switch (status_code) {
                    case 200:
                        $scope.errors = {};
                        $state.go("index");
                        break;
                    case 422:
                        $scope.errors = response.data.errors;
                        break;
                }
            }, function errorCallback(response) {
                console.log(JSON.stringify(response.data));
            });

        };
    }])
    .config(['$stateProvider', '$urlRouterProvider',
        function ($stateProvider, $urlRouterProvider) {

            $stateProvider
                .state('index', {
                    url: '/index',
                    templateUrl: '/static/templates/accounts.tpl.html',

                    controller: 'LoginCtrl'
                }).state('login', {
                url: '/login',
                templateUrl: '/static/templates/login.tpl.html',
                controller: 'LoginCtrl'
            }).state('subscribtion', {
                url: '/subscribtion',
                templateUrl: '/static/templates/subscribtion.tpl.html',
                controller: 'SubscribCtrl'
            })
        }
            ]);